import json
import csv 
import pandas as pd
import numpy as np


province_list = []
with open("../china.json") as f:
    chinamap = json.load(f)
    for pro in chinamap["features"]:
        province_list.append(pro["properties"]["name"])  
print(province_list)

# stockid2province
stockid2prov = {}
stock_frequency = {}
for pro in province_list:
    stock_frequency[pro] = 0
with open("../sample-data/stock_id2info.json") as f:
    stockinfo = json.load(f)
    for sid in stockinfo:
        for pro in province_list:
            if pro in stockinfo[sid][3]:
                stockid2prov[sid] = pro 
                stock_frequency[pro] += 1
                break
# print(stockid2prov)
# with open("../sample-data/stock_id2province.json", 'w') as f:
#     json.dump(stockid2prov, f, indent=4, ensure_ascii=False)
    
    
# statistics
df = pd.read_csv("../../executive_basic_info.csv")
df = df.drop_duplicates(subset='uid', keep='first')
def map2prov(x):
    try:
        return stockid2prov[str(x)]
    except:
        return np.nan
prov_df = df['stock_id'].apply(map2prov)
prov_df = prov_df.dropna()
people_frequency = prov_df.value_counts()
# print(people_frequency.to_dict())
# print(stock_frequency)

all_freq = {}
for pro in province_list:
    all_freq[pro] = {"people": int(people_frequency[pro]) if pro in people_frequency else 0,
                     "company": int(stock_frequency[pro]) if pro in stock_frequency else 0}
    
print(all_freq)

with open("../sample-data/map_data_counts.json", 'w') as f:
    json.dump(all_freq, f, indent=4, ensure_ascii=False)

